<?php


namespace App\Service;


use function PHPUnit\Framework\assertFalse;

class ContentCheck
{
    /**
     * @var mixed
     */
    private static $apiKey = '8Tvm9bTbQyuPaP4wmhfAnfEU';
    private static $secretKey = 'Djk7AGlmRCYtB5OaV8RyaK3s2j9G4y1C';

    /**
     * @param $text
     * @return mixed
     * 文本审核
     */
    public static function textCheck($text){
        if(empty($text)){
            return false;
        }
        $accessToken=self::getApiToken(self::$apiKey,self::$secretKey);
        $url="https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=$accessToken";
        $res=['text'=>$text];
        $result=self::request_post($url,$res);
        return json_decode($result,true)['conclusion'];
    }
    /**
     * @param $imgFile
     * @return mixed
     * 图片审核
     */
    public static function imgCheck($imgFile){
        if(empty($imgFile)){
            return false;
        }
        $accessToken=self::getApiToken(self::$apiKey,self::$secretKey);
        $url="https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=$accessToken";
        $img = file_get_contents($imgFile);
        $img = base64_encode($img);
        $res=['image'=>$img];
        $result=self::request_post($url,$res);
        return json_decode($result,true)['conclusion'];
    }

    /**
     * @param string $name
     * @param string $extId
     * @param string $videoUrl
     * @return mixed
     * 短视频审核
     */
    public static function videoCheck($name='',$extId='',$videoUrl=''){
        if(empty($text)||empty($extId)||empty($videoUrl)){
            return  service::jsonCode(400,'参数不足');
        }
        $accessToken=self::getApiToken(self::$apiKey,self::$secretKey);
        $url="https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined?access_token=$accessToken";
        $body = [
            'name' => $name,
            'extId' => $extId,
            'videoUrl' => $videoUrl
        ];
        $result=self::request_post($url,$body);
        return json_decode($result,true)['conclusion'];

    }
    /**
     * 发起http post请求(REST API), 并获取REST请求的结果
     * @param string $url
     * @param string $param
     * @return - http response body if succeeds, else false.
     */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }

    /**
     * @param string $grantType
     * @param string $apiKey
     * @param string $secretKey
     * @return bool|mixed
     * 获取调用接口的token值
     */
    public static function getApiToken($apiKey = '', $secretKey = '',$grantType = 'client_credentials')
    {
        $url = "https://aip.baidubce.com/oauth/2.0/token";
        $data = [
            'grant_type' => $grantType,
            'client_id' => $apiKey,
            'client_secret' => $secretKey
        ];
        $o='';
        foreach ($data as $k=>$v){
            $o.="$k=".urlencode($v)."&";
        }
        $postData=substr($o,0,-1);
        $res=self::request_post($url,$postData);
        $res=json_decode($res,true);
        if(isset($res['error'])){
            exit('API Key或者Secret Key不正确');
        }
        return $res['access_token'];
    }
}
